package cn.com.msui.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import cn.com.msui.dao.BaseDAO;
import cn.com.msui.domain.ViewItem;

/**
 * 通用服务类，该类中定义服务层通用方法
 * 
 * @author Yuegz
 * 
 */
public abstract class BaseService {

	protected BaseDAO dao;

	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}

	/**
	 * 分页条件转换方法
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> getPageCondition(ViewItem view) {
		Map<String, Object> pageCondition = new HashMap<String, Object>();
		int page = view.getPage();
		int rows = view.getRows();
		String sort = view.getSort();
		String order = view.getOrder();

		int skip = page > 0 ? (page - 1) * rows : 0;
		int count = rows;

		pageCondition.put("skip", skip);
		pageCondition.put("count", count);

		if (StringUtils.hasText(sort)) {
			if (!"desc".equals(order))
				order = "asc";
			pageCondition.put("sort", sort);
			pageCondition.put("order", order);
		}

		return pageCondition;
	}

	/**
	 * 获取数据总数
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int getTotalCount(Object obj) throws Exception {
		return dao.selectRowCount(obj);
	}
}
